#ifndef DO_MQTT_H
#define DO_MQTT_H

#include "MQTTClient.h"
#include <stdint.h>


/**
 * @brief MQTT client running status enumeration
 */
typedef enum mqtt_status {
  MQTT_CONN = 0,    // Connection phase
  MQTT_SUB,         // Subscription phase
  MQTT_PUB_MESSAGE, // Publish message phase
  MQTT_KEEPALIVE,   // Heartbeat maintenance phase
  MQTT_RECV,        // Receive message phase
  MQTT_ERR = 255    // Error phase
} MQTT_Status;

/**
 * @brief MQTT connection parameter structure
 */
typedef struct mqtt_connection {
  char mqtt_host_url[1024]; // Server URL
  uint8_t server_ip[4];     // Server IP address
  int port;                 // Server port number
  char clientid[1024];      // Client ID
  char username[1024];      // Username
  char passwd[1024];        // Password
  char pubtopic[255];       // Publish topic
  char pubtopic_reply[255]; // Publish reply topic
  char subtopic[255];       // Subscribe topic
  char subtopic_reply[255]; // Subscribe reply topic
  enum QoS pub_qos;         // Publish message QoS level
  enum QoS sub_qos;         // Subscribe message QoS level
  char willtopic[255];      // Will topic
  enum QoS will_qos;        // Will message QoS level
  char willmsg[255];        // Will message content
} mqttconn;

/**
 * @brief Initialize MQTT client
 * @param sn: socket number
 * @param send_buf: send buffer pointer
 * @param recv_buf: receive buffer pointer
 * @note Complete DNS resolution, network connection and MQTT parameter
 * initialization
 */
void mqtt_init(uint8_t sn, uint8_t *send_buf, uint8_t *recv_buf);

/**
 * @brief Update MQTT system tick count
 */
void mqtt_tick(void);

/**
 * @brief Get current system tick count
 * @return Current tick count value
 */
uint32_t get_tick_count(void);

/**
 * @brief Execute MQTT state machine logic (connect, subscribe, publish, etc.)
 */
void do_mqtt(void);

#endif // __DO_MQTT_H__
